import { invtPrintPDF, invtPrintSwPdf } from '@/api/invt'
import PrintModal from '../components/print-modal'

export default {
    components: {
        PrintModal
    },
    data () {
        return {
            printModal: {
                visible: false,
                id: '',
                options: [
                    {
                        value: 'INVT',
                        label: '核注清单',
                        checked: true
                    },
                    {
                        value: 'SWPDF',
                        label: '复核表',
                        checked: false
                    }
                ]
            }
        }
    },
    methods: {
        // 打印
        printInvt (id) {
            this.printModal.id = id
            this.printModal.visible = true
        },
        queryInvtFile (type) {
            let arr = []
            let keys = type.split(',')
            const swIndex = keys.findIndex(k => k == 'SWPDF')
            if (swIndex >= 0) {
                keys.splice(swIndex, 1)
                arr.push(invtPrintSwPdf(this.printModal.id))
            }
            if (keys.length) {
                arr.push(invtPrintPDF(this.printModal.id, keys.join(',')))
            }
            return Promise.all(arr).then(allRes => {
                let list = []
                let responseMsg = []
                allRes.forEach(res => {
                    if (res.data.code === '0000') {
                        const data = res.data.data
                        if (
                            Object.prototype.toString.call(data) === '[object Array]'
                        ) {
                            Array.prototype.push.apply(list, data)
                        } else if (data.url) {
                            list.push(data.url)
                        }
                    } else {
                        responseMsg.push(res.data.msg)
                    }
                })
                let result = {
                    data: {
                        isSuccess: list.length > 0,
                        data: list,
                        responseMsg: responseMsg.join('<br>')
                    }
                }
                if (result.data.code === '0000' && result.data.msg) {
                    this.$Message.error({
                        content: result.data.msg,
                        duration: 4
                    })
                }
                return result
            })
        }
    }
}
